package tpcomp2010;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import tpcomp2010.estructura.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
import javax.swing.JFrame;
import org.jgraph.graph.DefaultGraphModel;


/* Pantalla principal de la aplicacion de Generacion de AFN, AFD y AFD min desde
 * expresiones regulares.
 * 
 * @author Tania Nuñez
 * @autor Victor Ayala
 */
public class principal extends javax.swing.JFrame {

    Map<String, String> mapa = new HashMap<String, String>();
    Automata AFN, AFD, AFDmin;

    /** 
     * Constructor de la clase principal.
     */
    public principal() {
        //"Ninguno", "[a-z]", "[A-Z]", "[a-zA-Z]", "[0-9]", "Todos"

        mapa.put("Ninguno", "");
        mapa.put("[a-z]", "abcdefghijklmnopqrstuvwxyz");
        mapa.put("[A-Z]", "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        mapa.put("[a-zA-Z]", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        mapa.put("[0-9]", "0123456789");
        mapa.put("Todos", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jTextER = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jTextAlfa = new javax.swing.JTextField();
        jComboBox1 = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        mensajes = new javax.swing.JTextPane();
        textValidar = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        afnButton = new javax.swing.JButton();
        afdButton = new javax.swing.JButton();
        afdminButton = new javax.swing.JButton();
        pertenece = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        afnGrafButton = new javax.swing.JButton();
        afdGrafButton = new javax.swing.JButton();
        afdminGrafButton = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        resAFN = new javax.swing.JTextArea();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        resAFD = new javax.swing.JTextArea();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        resAFDMin = new javax.swing.JTextArea();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        resSimulacion = new javax.swing.JTextArea();
        jLabel5 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Expresion Regular");

        jLabel2.setText("Alfabeto");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Ninguno", "[a-z]", "[A-Z]", "[a-zA-Z]", "[0-9]", "Todos" }));
        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jButton1.setText("Hazlo!");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        mensajes.setEditable(false);
        mensajes.setEnabled(false);
        jScrollPane1.setViewportView(mensajes);

        textValidar.setEnabled(false);

        jLabel3.setText("Validar cadena:");

        afnButton.setText("AFN");
        afnButton.setEnabled(false);
        afnButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afnButtonActionPerformed(evt);
            }
        });

        afdButton.setText("AFD");
        afdButton.setEnabled(false);
        afdButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afdButtonActionPerformed(evt);
            }
        });

        afdminButton.setText("AFDmin");
        afdminButton.setEnabled(false);
        afdminButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afdminButtonActionPerformed(evt);
            }
        });

        jLabel4.setText("Graficar:");

        afnGrafButton.setText("AFN");
        afnGrafButton.setEnabled(false);
        afnGrafButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afnGrafButtonActionPerformed(evt);
            }
        });

        afdGrafButton.setText("AFD");
        afdGrafButton.setEnabled(false);
        afdGrafButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afdGrafButtonActionPerformed(evt);
            }
        });

        afdminGrafButton.setText("AFDmin");
        afdminGrafButton.setEnabled(false);
        afdminGrafButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                afdminGrafButtonActionPerformed(evt);
            }
        });

        jButton2.setText("Clean");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText("Salir");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        resAFN.setColumns(20);
        resAFN.setEditable(false);
        resAFN.setRows(5);
        jScrollPane2.setViewportView(resAFN);

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("AFN", jPanel1);

        resAFD.setColumns(20);
        resAFD.setEditable(false);
        resAFD.setRows(5);
        jScrollPane3.setViewportView(resAFD);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 445, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 120, Short.MAX_VALUE)
            .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("AFD", jPanel2);

        resAFDMin.setColumns(20);
        resAFDMin.setEditable(false);
        resAFDMin.setRows(5);
        jScrollPane4.setViewportView(resAFDMin);

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 445, Short.MAX_VALUE)
            .addGap(0, 445, Short.MAX_VALUE)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 120, Short.MAX_VALUE)
            .addGap(0, 120, Short.MAX_VALUE)
            .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("AFD min", jPanel3);

        resSimulacion.setColumns(20);
        resSimulacion.setEditable(false);
        resSimulacion.setRows(5);
        jScrollPane5.setViewportView(resSimulacion);

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 445, Short.MAX_VALUE)
            .addGap(0, 445, Short.MAX_VALUE)
            .addGap(0, 445, Short.MAX_VALUE)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 120, Short.MAX_VALUE)
            .addGap(0, 120, Short.MAX_VALUE)
            .addGap(0, 120, Short.MAX_VALUE)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 120, Short.MAX_VALUE)
        );

        jTabbedPane1.addTab("Simulacion", jPanel4);

        jLabel5.setText("Mensajes");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jTextAlfa)
                            .addComponent(jTextER, javax.swing.GroupLayout.DEFAULT_SIZE, 255, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, 94, Short.MAX_VALUE)
                            .addComponent(jComboBox1, 0, 94, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addGap(10, 10, 10)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(afnButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(afdButton)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addComponent(afdminButton))
                                    .addComponent(textValidar)))
                            .addComponent(jLabel4)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(afnGrafButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(afdGrafButton)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(afdminGrafButton)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, 83, Short.MAX_VALUE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, 80, Short.MAX_VALUE))
                            .addComponent(pertenece, javax.swing.GroupLayout.DEFAULT_SIZE, 169, Short.MAX_VALUE))
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addContainerGap(415, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE)
                            .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 450, Short.MAX_VALUE))
                        .addContainerGap())))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jTextAlfa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextER, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addGap(18, 18, 18)
                .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 148, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jLabel5)
                .addGap(3, 3, 3)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(textValidar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(afnButton)
                            .addComponent(afdButton)
                            .addComponent(afdminButton)))
                    .addComponent(pertenece, javax.swing.GroupLayout.DEFAULT_SIZE, 49, Short.MAX_VALUE))
                .addGap(15, 15, 15)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(afnGrafButton)
                    .addComponent(afdGrafButton)
                    .addComponent(afdminGrafButton)
                    .addComponent(jButton3)
                    .addComponent(jButton2))
                .addContainerGap())
        );

        jTabbedPane1.getAccessibleContext().setAccessibleName("tPanel");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        jTextAlfa.setText(mapa.get((String) jComboBox1.getSelectedItem()));
    }//GEN-LAST:event_jComboBox1ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        limpiar();
        if (jTextAlfa.getText().isEmpty() || jTextER.getText().isEmpty()) {
            mensajes.setText("Debe ingresar la expresion regular y el alfabeto.");
            return;
        }
        String s = null;
        for (int i = 0; i < jTextAlfa.getText().length(); i++) {
            s = jTextAlfa.getText().substring(i, i + 1);
            if (s.matches("[a-zA-Z0-9]|\\d") == false){
                mensajes.setText("El alfabeto ingresado NO es valido. \n");
                return;
            }
        }
        this.procesarER();
    }//GEN-LAST:event_jButton1ActionPerformed

    /**
     * Funcion que inicia el proceso de la Expresion Regular
     */
    private void procesarER() {
        try {
            String alfabeto = jTextAlfa.getText();
            InfijaPostfija itop = new InfijaPostfija(jTextER.getText(), alfabeto);
            String cadenaPostFija = itop.getPostfixExp();

            if (itop.ok) { //si se proceso correctamente la ER

                String str;
                Stack<Automata> stack;

                ArrayList<String> al = new ArrayList<String>();
                for (int i = 0; i < alfabeto.length(); i++) {
                    al.add(alfabeto.substring(i, i + 1));
                }

                stack = new Stack<Automata>();
                Automata autoUnario = new Automata();
                Automata autoOp1;
                Automata autoOp2;
                for (int i = 0; i < cadenaPostFija.length(); i++) {
                    str = cadenaPostFija.substring(i, i + 1);
                    if (alfabeto.indexOf(str) != -1) {
                        Automata autoOperando = new Automata(str);
                        stack.push(autoOperando);
                    } else if (InfijaPostfija.isOperator(str)) {
                        if (str.equals("*")) {
                            autoUnario = stack.pop();
                            autoUnario.asterisco();
                            stack.push(autoUnario);
                        } else if (str.equals("+")) {
                            autoUnario = stack.pop();
                            autoUnario.mas();
                            stack.push(autoUnario);
                        } else if (str.equals("?")) {
                            autoUnario = stack.pop();
                            autoUnario.ceroUno();
                            stack.push(autoUnario);
                        } else if (str.equals(".")) {
                            autoOp1 = stack.pop();
                            autoOp2 = stack.pop();
                            autoOp2.concatenacion(autoOp1);
                            stack.push(autoOp2);
                        } else if (str.equals("|")) {
                            autoOp1 = stack.pop();
                            autoOp2 = stack.pop();
                            autoOp2.or(autoOp1);
                            stack.push(autoOp2);
                        }
                    }
                }
                AFN = stack.pop();
                AFN.setAlfabeto(al);
                AFN.setExpRegular(jTextER.getText());

                System.out.println(AFN.imprimirAutomata());
                resAFN.setText(resAFN.getText() + "\n\n\n#### GENERACION DEL AFN ####\n");
                resAFN.setText(resAFN.getText() + AFN.imprimirAutomata());

                SubConjunto algSub;
                MatrizTransicion dtran;
                algSub = new SubConjunto(AFN);
                dtran = algSub.ejecutar();

                System.out.println(dtran.imprimirMatriz());
                resAFD.setText(resAFD.getText() + "\n\n\n#### GENERACION DEL AFD ####\n");
                resAFD.setText(resAFD.getText() + dtran.imprimirMatriz());

                AFD = dtran.convertAutomata();
                AFD.setAlfabeto(al);
                AFD.setExpRegular(jTextER.getText());

                Minimizacion m = new Minimizacion(AFD);

                AFDmin = m.minimizar();

                System.out.println("");
                System.out.println(AFDmin.imprimirAutomata());
                resAFDMin.setText(resAFDMin.getText() + "\n\n\n#### GENERACION DEL AFD MINIMO ####\n");
                resAFDMin.setText(resAFDMin.getText() + AFDmin.imprimirAutomata());

                habilitarValidacion();
                habilitarGrafico();

            } else {
                mensajes.setText("Error al analizar la Expresion Regular. Hay caracteres que no son validos o no petenecen al alfabeto.");
            }

        } catch (Exception ex) {
            Logger.getLogger(principal.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void afnButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afnButtonActionPerformed
        Simulacion s = new Simulacion(AFN, TipoAutomata.AFN);
        if (s.ejecutar(textValidar.getText())) {
            perteneceOK();
            resSimulacion.setText(s.getCamino());
        } else {
            perteneceNO();
        }
    }//GEN-LAST:event_afnButtonActionPerformed

    private void afdButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afdButtonActionPerformed
        Simulacion s = new Simulacion(AFD, TipoAutomata.AFD);
        if (s.ejecutar(textValidar.getText())) {
            resSimulacion.setText(s.getCamino());
            perteneceOK();
        } else {
            perteneceNO();
        }
    }//GEN-LAST:event_afdButtonActionPerformed

    private void afdminButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afdminButtonActionPerformed
        Simulacion s = new Simulacion(AFDmin, TipoAutomata.AFD);
        if (s.ejecutar(textValidar.getText())) {
            perteneceOK();
            resSimulacion.setText(s.getCamino());
        } else {
            perteneceNO();
        }
    }//GEN-LAST:event_afdminButtonActionPerformed

    private void afnGrafButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afnGrafButtonActionPerformed
        try {
            DefaultGraphModel model = new DefaultGraphModel();
            Grafo ag = new Grafo(AFN, model);

            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            f.getContentPane().add(ag);
            f.pack();
            f.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Grafo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_afnGrafButtonActionPerformed

    private void afdGrafButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afdGrafButtonActionPerformed
        try {
            DefaultGraphModel model = new DefaultGraphModel();
            Grafo ag = new Grafo(AFD, model);

            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            f.getContentPane().add(ag);
            f.pack();
            f.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Grafo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_afdGrafButtonActionPerformed

    private void afdminGrafButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_afdminGrafButtonActionPerformed
        try {
            DefaultGraphModel model = new DefaultGraphModel();
            Grafo ag = new Grafo(AFDmin, model);

            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            f.getContentPane().add(ag);
            f.pack();
            f.setVisible(true);
        } catch (Exception ex) {
            Logger.getLogger(Grafo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_afdminGrafButtonActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        limpiarTodo();

    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton3ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new principal().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton afdButton;
    private javax.swing.JButton afdGrafButton;
    private javax.swing.JButton afdminButton;
    private javax.swing.JButton afdminGrafButton;
    private javax.swing.JButton afnButton;
    private javax.swing.JButton afnGrafButton;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JTextField jTextAlfa;
    private javax.swing.JTextField jTextER;
    private javax.swing.JTextPane mensajes;
    private javax.swing.JLabel pertenece;
    private javax.swing.JTextArea resAFD;
    private javax.swing.JTextArea resAFDMin;
    private javax.swing.JTextArea resAFN;
    private javax.swing.JTextArea resSimulacion;
    private javax.swing.JTextField textValidar;
    // End of variables declaration//GEN-END:variables

    /**
     * habilita componentes para validacion
     */
    private void habilitarValidacion() {
        textValidar.setEnabled(true);
        afnButton.setEnabled(true);
        afdButton.setEnabled(true);
        afdminButton.setEnabled(true);
    }

    /**
     * Deshabilita componentes para validacion
     */
    private void deshabilitarValidacion() {
        textValidar.setEnabled(false);
        afnButton.setEnabled(false);
        afdButton.setEnabled(false);
        afdminButton.setEnabled(false);
    }

    /**
     * deshabilita componentes para grafico
     */
    private void deshabilitarGrafico() {
        afnGrafButton.setEnabled(false);
        afdGrafButton.setEnabled(false);
        afdminGrafButton.setEnabled(false);
    }

    /**
     * habilita componentes para grafico
     */
    private void habilitarGrafico() {
        afnGrafButton.setEnabled(true);
        afdGrafButton.setEnabled(true);
        afdminGrafButton.setEnabled(true);
    }

    /**
     * limpia los componentes principales
     */
    private void limpiar() {
        textValidar.setText("");
        pertenece.setIcon(null); // NOI18N
        pertenece.setText("");
        mensajes.setText("");
        deshabilitarValidacion();
        deshabilitarGrafico();
        resAFD.setText("");
        resAFDMin.setText("");
        resAFN.setText("");
        resSimulacion.setText("");
    }

    /**
     * Limpia todos los componentes
     */
    private void limpiarTodo() {
        jTextAlfa.setText("");
        jTextER.setText("");
        textValidar.setText("");
        pertenece.setIcon(null); // NOI18N
        pertenece.setText("");
        mensajes.setText("");
        resAFD.setText("");
        resAFDMin.setText("");
        resAFN.setText("");
        resSimulacion.setText("");
        deshabilitarValidacion();
        deshabilitarGrafico();
    }

    /**
     * Muestra el icono "OK" de acuerdo al resultado
     * de la simulacion
     */
    private void perteneceOK() {
        pertenece.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tpcomp2010/imagen/ok.png"))); // NOI18N
        pertenece.setText("Pertenece!");
    }

    /**
     * Muestra el icono "ERROR" de acuerdo al resultado
     * de la simulacion
     */
    private void perteneceNO() {
        pertenece.setIcon(new javax.swing.ImageIcon(getClass().getResource("/tpcomp2010/imagen/no.png"))); // NOI18N
        pertenece.setText("NO Pertenece!");
    }
}
